package com.sgcc.sx.pmos.common.utils;

import android.app.Activity;

import com.blankj.utilcode.util.ToastUtils;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

public class XXPermissionsUtil {

    public interface PermissionCallback {

        void Call();
    }

    public static void requestPermissions(Activity activity, final boolean isRepe, final PermissionCallback
            callback, final String... permissions) {
        XXPermissions xxPermissions = XXPermissions.with(activity);
        // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
        if (isRepe) {
            xxPermissions.constantRequest();
        }
        // 支持请求6.0悬浮窗权限8.0请求安装权限
        //.permission(Permission.REQUEST_INSTALL_PACKAGES)
        // 不指定权限则自动获取清单中的危险权限
        xxPermissions.permission(permissions).request(new OnPermission() {

            @Override
            public void hasPermission(List<String> granted, boolean isAll) {
                if (isAll) {
                    callback.Call();
                } else {
//                    callback.Fail();

                    ToastUtils.showShort("获取权限失败");
                }
            }

            @Override
            public void noPermission(List<String> denied, boolean quick) {
                if (quick) {
                    ToastUtils.showShort("被永久拒绝授权，请手动授予权限");
                } else {
                    ToastUtils.showShort("获取权限失败");
                }
            }
        });
    }


    /**
     * 添加手机联系人列表页面
     *
     * @param activity
     */

    public static void startContactActivity(Activity activity) {

        XXPermissionsUtil.requestPermissions(activity, false, new PermissionCallback() {
            @Override
            public void Call() {
            }

        }, Permission.READ_CONTACTS);

    }


    /**
     * 拍照
     *
     * @param activity
     */
    public static void camera(Activity activity, PermissionCallback callback) {
        XXPermissionsUtil.requestPermissions(activity, false, callback, Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE);
    }

    /**
     * 录像
     *
     * @param activity
     */
    public static void recordVideo(Activity activity, PermissionCallback callback) {
        XXPermissionsUtil.requestPermissions(activity, false, callback,
                Permission.CAMERA, Permission.RECORD_AUDIO, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE);
    }


    /**
     * 位置
     *
     * @param activity
     */
    public static void location(Activity activity, PermissionCallback callback) {
        XXPermissionsUtil.requestPermissions(activity, false, callback, Permission.ACCESS_FINE_LOCATION);
    }

    /**
     * 电话
     *
     * @param activity
     */
    public static void callPhone(Activity activity, PermissionCallback callback) {
        XXPermissionsUtil.requestPermissions(activity, false, callback, Permission.CALL_PHONE);
    }


    /**
     * 文件读取
     *
     * @param activity
     */
    public static void externalStorage(Activity activity, PermissionCallback callback) {
        XXPermissionsUtil.requestPermissions(activity, false, callback, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE);
    }
}